function Y = deploy(X, hDeploy, block_size, border_size)

% thresh = 5000;
% thresh = 90000;
% thresh = 90000*2;

if nargin < 3, block_size = [100 100 18]; end
if nargin < 4, border_size = [10 10]; end

% block_size = [30 30];
% border_size = [5 5];
% X = single(X);

% X = im2double(X);

% if all(size(X) <= block_size(1 : 2) * 1.5)
%     Y = hDeploy(X);
%     return
% end

if ndims(X) == 3
    Y = srimg.process(X, hDeploy, 1200);
    return
end

%% deploy blocks
% tic
[Z, nums] = srimg.img2blocks(X, block_size(1 : 2), border_size);
Y = srimg.process(Z, hDeploy, block_size(3));
% toc

%% rebuild images
if ~iscell(X)
    X_size = size(X);
else
    X_size = cellfun(@(x)size(x), X, 'UniformOutput', false);
    X_size = cat(1, X_size{:});
end

scale = size(Y) ./ size(Z);
X_size = bsxfun(@times, X_size, scale(1 : 2));

Y = srimg.blocks2img(Y, nums, X_size, border_size .* scale(1:2));

return

%%
%{
c = ceil(size(X) / 2);
Patches = {
    X(1:c(1)+rf(1), 1:c(2)+rf(2))     X(1:c(1)+rf(1), c(2)-rf(2)+1:end)
    X(c(1)-rf(1)+1:end, 1:c(2)+rf(2)) X(c(1)-rf(1)+1:end, c(2)-rf(2)+1:end)
    };
bFirst = true;  shifts = [0 0];
for pi = 1 : size(Patches, 1)
    shifts(1) = rf(1) * (pi-1);
    for pj = 1 : size(Patches, 2)
        shifts(2) = rf(2) * (pj-1);
        patch = Patches{pi, pj};
        if numel(patch) > thresh
            Y = srmodel.deploy(hDeploy, patch);
        else
            Y = hDeploy(patch);
        end
        if bFirst
            bFirst = false;
            scale = size(Y) ./ size(patch);
            s = scale .* c;  rf = rf .* scale;
        end
        s1 = s(1)+shifts(1); s2 = s(2)+shifts(2);
        if pi == size(Patches, 2), s1 = size(Y, 1); end
        if pj == size(Patches, 2), s2 = size(Y, 2); end
        Patches{pi, pj} = Y(1+shifts(1) : s1, 1+shifts(2) : s2);
    end
end

Y = [
    Patches{1, :}
    Patches{2, :}
    ];
%}
%%
%{
thresh = 5000;
% thresh = 90000*2;
% rf = 20;
[~, ~, ImgNum] = size(X);
if ImgNum < thresh
    Y = hDeploy(X);
    return
end

h = waitbar(0, 'Please wait ...');
GroupSize = ceil(ImgNum / thresh);
pp = 0; g = 0;
while pp(end) < ImgNum
    g = g + 1;
    waitbar(g / GroupSize);
    
    pp = pp(end)+1 : pp(end)+thresh;
    if pp(end) > ImgNum
        pp = pp(1) : ImgNum;
    end
    % y = X(:, :, pp);
    y = hDeploy(X(:, :, pp));
    if pp(1) == 1
        s = size(y);
        Y = zeros([s(1:2) ImgNum]);
    end
    Y(:, :, pp) = y;
end
close(h)
%}
